<?php
class Blog extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		$this->load->model ( 'blog_model' );
		$this->load->model ( 'user_model' );
		$this->load->model ( 'tag_model' );
		$this->load->model ( 'like_model' );
		$this->load->model ( 'feed_model' );
		$this->load->model ( 'reply_model' );
		$this->load->model ( 'follow_model' );
		$this->load->library ( 'datelib' );
	}
	public function index($loc = 'home') {
		$uid = $this->session->userdata ( 'user_id' );
		$order = $this->input->get ( 'order' );
		
		if (! $uid && $loc != 'home') {
			redirect ( 'blog', 'refresh' );
		}
		
		$data = array (
				'title' => 'Blog' 
		);
		
		if ($uid||$loc!='home') {
			$data ['user_info'] = $this->user_model->get_user ( $uid );
			$this->load->view ( 'template/header', $data );
			
			if ($this->blog_model->check_own ( $uid, $loc ) || $this->blog_model->check_manage ( $uid, $loc ))
				$this->session->set_userdata ( 'blog_id', $loc );
			
			$this->set_post ();
			
			$order = $this->input->get ( 'order' );
			if ($order != 'asc') {
				$order = 'desc';
			}
			
			$this->load->view ( 'framework/main', array (
					'loc' => $loc,
					'main_url' => 'index.php/blog/blog_area?loc=' . $loc . '&order=' . $order,
					'js_load' => array (
							"public/script/feed.js",
							"public/script/like.js",
							"public/script/reply.js",
							"public/script/follow.js",
							"public/script/delete_feed.js",
							"public/script/share.js",
							'public/script/dashboard.js'
					) 
			) );
		} else {
			$this->load->view ( 'template/header', $data );
			$this->load->view ( 'app/intro' );
		}
		$this->load->view ( 'template/footer' );
	}
	public function navi_bar($loc = 'home') {
		$uid = $this->session->userdata ( 'user_id' );
		
		$blog_list ['own'] = $this->blog_model->get_own_blog_list ( $uid );
		$blog_list ['manage'] = $this->blog_model->get_manage_blog_list ( $uid );
		
		$data ['blogs'] = $blog_list;
		$data ['current'] = $loc;
		$data ['focus_tags'] = $this->tag_model->get_focus_tags ( $uid );
		$data ['cur_user'] = $this->user_model->get_user ( $uid );
		
		if($loc!='home'&&$loc!='like'){
			$blog = $this->blog_model->get_blog ( $loc );
			$data ['blog'] = $this->blog_model->get_blog ( $loc );
			$data ['blog'] ['creator'] = $this->blog_model->get_creator ( $loc );
			$data ['blog'] ['followed'] = $this->follow_model->is_follow ( $uid, $loc );
			$data ['blog'] ['followers'] = $this->follow_model->get_followers ( $loc );
			$data ['blog'] ['article_num'] = count($this->feed_model->home_get ( $uid - 0 ));
		}
		
		$data ['managed_blog'] = ($loc !== 'home') && ($this->blog_model->check_own ( $uid, $loc ) || $this->blog_model->check_manage ( $uid, $loc ));
		
		$this->load->view ( 'template/navi', $data );
	}
	public function blog_area() {
		$uid = $this->session->userdata ( 'user_id' );
		
		$loc = $this->input->get ( 'loc' );
		$order = $this->input->get ( 'order' );
		if ($order == '') {
			$oder = 'desc';
		}
		
		$data ['loc'] = $loc;
		$data ['managed_blog'] = false;
		$data ['cur_user'] = $this->user_model->get_user ( $uid );
		
		if ($loc == 'home') {
			$feeds = $this->feed_model->home_get ( $uid - 0 );
			$data ['feeds'] = $this->attach_detail ( $feeds );
			$data ['focus_tags'] = $this->tag_model->get_focus_tags ( $uid );
		} elseif ($loc == 'like') {
			$feeds = $this->like_model->get_like_post ( $uid - 0 );
			$data ['feeds'] = $this->attach_detail ( $feeds );
		} elseif ($loc == 'discover') {
			$feeds = $this->feed_model->random_get ( 5, $uid );
			$data ['feeds'] = $this->attach_detail ( $feeds );
		} else {
			$data ['managed_blog'] = ($this->blog_model->check_own ( $uid, $loc ) || $this->blog_model->check_manage ( $uid, $loc ));
			$blog = $this->blog_model->get_blog ( $loc );
			$data ['blog'] = $this->blog_model->get_blog ( $loc );
			$data ['blog'] ['creator'] = $this->blog_model->get_creator ( $loc );
			$data ['blog'] ['followed'] = $this->follow_model->is_follow ( $uid, $loc );
			$data ['blog'] ['followers'] = $this->follow_model->get_followers ( $loc );
			$feeds = $this->feed_model->get_all_posts ( $loc, $order );
			$data ['feeds'] = $this->attach_detail ( $feeds );
		}
		$this->load->view ( 'reply/post_reply' );
		$this->load->view ( 'blog/feed', $data );
		
		$blog_list ['own'] = $this->blog_model->get_own_blog_list ( $uid, true );
		$blog_list ['manage'] = $this->blog_model->get_manage_blog_list ( $uid, true );
		$this->load->view ( 'feed/share', $blog_list );
	}
	public function manage() {
		$uid = $this->session->userdata ( 'user_id' );
		
		if ($uid) {
			$data ['user_info'] = $this->user_model->get_user ( $uid );
			$this->load->view ( 'template/header', $data );
			
			$this->load->view ( 'framework/main', array (
					'loc' => 'blog_manage',
					'main_url' => 'index.php/blog/dashboard',
					'js_load' => array (
							'public/script/dashboard.js' 
					) 
			) );
			
			$this->load->view ( 'blog/new' );
			$this->load->view ( 'blog/edit' );
			
			$this->set_post ();
			
			$this->load->view ( 'template/footer' );
		} else {
			redirect ( 'blog', 'refresh' );
		}
	}
	
	public function dashboard($bid) {
		$this->load->view ( 'template/footer' );
	}
	
	public function dashboard_old() {
		$uid = $this->session->userdata ( 'user_id' );
		
		if ($uid) {
			$blog_list ['own'] = $this->blog_model->get_own_blog_list ( $uid, true );
			$blog_list ['manage'] = $this->blog_model->get_manage_blog_list ( $uid, true );
			$data ['blogs'] = $blog_list;
			$data ['user'] = $this->user_model->get_user ( $uid );
			$this->load->view ( 'blog/dashboard', $data );
		} else {
			echo 'please sign in';
		}
	}
	public function create() {
		$uid = $this->session->userdata ( 'user_id' );
		$bname = $this->input->post ( 'bname' );
		if ($bname) {
			$this->blog_model->create_blog ( $bname, $uid );
			echo 'pass';
		} else {
			echo 'fail';
		}
	}
	public function delete() {
		$uid = $this->session->userdata ( 'user_id' );
		$bid = $this->input->post ( 'bid' );
		if ($this->blog_model->check_own ( $uid, $bid )) {
			$this->blog_model->delete ( $bid );
		}
	}
	public function add_editor() {
		$uid = $this->session->userdata ( 'user_id' );
		$bid = $this->input->post ( 'bid' );
		$editor_id = $this->input->post ( 'editor_id' );
		if ($editor_id == $uid) {
			echo fail;
		}
		if ($this->blog_model->check_own ( $uid, $bid )) {
			$this->blog_model->add_editor ( $bid, $editor_id );
			echo 'pass';
		} else {
			echo 'fail';
		}
	}
	public function delete_editor() {
		$uid = $this->session->userdata ( 'user_id' );
		$bid = $this->input->post ( 'bid' );
		$editor_id = $this->input->post ( 'editor_id' );
		if ($editor_id == $uid) {
			echo fail;
		}
		if ($this->blog_model->check_own ( $uid, $bid )) {
			$this->blog_model->remove_editor ( $bid, $editor_id );
			echo 'pass';
		} else {
			echo 'fail';
		}
	}
	private function set_post() {
		$uid = $this->session->userdata ( 'user_id' );
		$bid = $this->session->userdata ( 'blog_id' );
		$binfo = $this->blog_model->get_blog ( $bid );
		$blogdata ['blog_name'] = $binfo ['bname'];
		
		$blogdata ['own'] = $this->blog_model->get_own_blog_list ( $uid );
		$blogdata ['manage'] = $this->blog_model->get_manage_blog_list ( $uid );
		$this->load->view ( 'feed/post', $blogdata );
	}
	private function attach_detail(&$posts) {
		$uid = $this->session->userdata ( 'user_id' );
		foreach ( $posts as &$feed ) {
			$aid = $feed ['author'];
			$bid = $feed ['bid'];
			$feed ['author'] = $this->user_model->get_user ( $aid );
			$cur_user = $this->user_model->get_user ( $uid );
			if ($feed ['author'] ['uname'] == $cur_user ['uname']) {
				$feed ['author'] ['uname'] = '<strong>you</strong>';
			}
			$blog = $this->blog_model->get_blog ( $bid );
			$feed ['blog_name'] = $blog ['bname'];
			$feed ['tags'] = $this->tag_model->get_tags_array ( $feed ['pid'] );
			$feed ['islike'] = $this->like_model->islike ( $uid, $feed ['pid'] );
			$feed ['num_likes'] = $this->like_model->likes_num ( $feed ['pid'] );
			$feed ['num_reply'] = $this->reply_model->get_num_reply ( $feed ['pid'] );
			$feed ['num_share'] = $this->feed_model->get_share_num ( $feed ['pid'] );
			if ($feed ['type'] == 'share') {
				$feed ['orig'] = $this->feed_model->get ( $feed ['content'] );
				if ($feed ['orig']) {
					$orig_arr = array (
							$feed ['orig'] 
					);
					$orig_arr = $this->attach_detail ( $orig_arr );
					$feed ['orig'] = $orig_arr [0];
				} else {
					$feed ['orig'] ['type'] = 'delete';
				}
			}
		}
		return $posts;
	}
}

?>